#include <QHostInfo>
#include "data_processor.h"

Data_processor::Data_processor(QObject *parent) : QObject(parent)
{

}

void Data_processor::sl_get_host_info(bool flag)
{
    QString host_name(QHostInfo::localHostName());
    QHostInfo host_info(QHostInfo::fromName(host_name));

    QStringList string_list = QStringList();
    string_list.append(host_name + "\n");

    QList<QHostAddress> address_list = host_info.addresses();
    if ( !address_list.isEmpty()) {
        for (int i(0); address_list.count() != i; ++i) {
            QHostAddress host = address_list.at(i);

            // 用户选择只显示IPv4协议
            if (flag) {
                flag = (QAbstractSocket::IPv4Protocol == host.protocol());
            } else {
                flag = true;
            }

            if (flag) {
                string_list.append("协 议：" + protocol_name(host.protocol()));
                string_list.append("本机IP地址：" + host.toString());
            }
        }
    }

    emit si_host_info(string_list);
}

QString Data_processor::protocol_name(QAbstractSocket::NetworkLayerProtocol protocol)
{
    QString ip_content;

    switch (protocol) {
    case QAbstractSocket::IPv4Protocol:
        ip_content = "IPv4 Protocol";
        break;

    case QAbstractSocket::IPv6Protocol:
        ip_content = "IPv6 Protocol";
        break;

    case QAbstractSocket::AnyIPProtocol:
        ip_content = "Any IP Protocol";
        break;

    default:
        ip_content = "Unknown Network Layer Protocol";
    }

    return ip_content;
}
